package com.mxpio.erp.quality.service.impl;

import com.mxpio.erp.common.quality.entity.InspectionScheme;
import com.mxpio.erp.common.quality.entity.InspectionSchemeChain;
import com.mxpio.erp.common.quality.entity.InspectionSchemeLine;
import com.mxpio.erp.common.quality.service.InspectionSchemeChainService;
import com.mxpio.erp.common.technology.entity.Item;
import com.mxpio.erp.common.technology.entity.ItemGroup;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
public class InspectionSchemeChainServiceImpl extends BaseServiceImpl<InspectionSchemeChain> implements InspectionSchemeChainService {

    /*@Override
    @Transactional(readOnly = true)
    public Page<InspectionScheme> pageByScheme(Pageable pageAble, Criteria c) {
        Page<InspectionScheme> page = JpaUtil.linq(InspectionScheme.class).where(c).paging(pageAble);
        Set<String> schemeCodes = JpaUtil.collect(page.getContent(), "code");
        List<InspectionSchemeChain> chains = JpaUtil.linq(InspectionSchemeChain.class)
                .collect("itemCode", Item.class,"itemCode")
                .exists(InspectionScheme.class)
                .equalProperty("schemeCode", "code")
                .equal("busiType","1")
                .end()
                .in("schemeCode", schemeCodes).list();
        Map<String,List<InspectionSchemeChain>> chainMap = new HashMap<>();
        for(InspectionSchemeChain chain : chains){
            chainMap.computeIfAbsent(chain.getSchemeCode(), k -> new ArrayList<InspectionSchemeChain>());
            chainMap.get(chain.getSchemeCode()).add(chain);
        }
        for(InspectionScheme supply : page.getContent()){
            supply.setInspectionSchemeChains(chainMap.get(supply.getCode()));
        }
        return page;
    }*/

    @Override
    @Transactional(readOnly = true)
    public Page<Item> pageByItem(Pageable pageAble, Criteria c) {
        Page<Item> page = JpaUtil.linq(Item.class).where(c).paging(pageAble);
        Set<String> itemCodes = JpaUtil.collect(page.getContent(), "itemCode");
        List<InspectionSchemeChain> chains = JpaUtil.linq(InspectionSchemeChain.class)
                .collect("code", InspectionScheme.class,"schemeCode")
                .equal("busiType","1")
                .in("itemCode", itemCodes).list();
        Map<String,List<InspectionSchemeChain>> chainMap = new HashMap<>();
        for(InspectionSchemeChain chain : chains){
            chainMap.computeIfAbsent(chain.getItemCode(), k -> new ArrayList<InspectionSchemeChain>());
            chainMap.get(chain.getItemCode()).add(chain);
            InspectionScheme inspectionScheme = chain.getInspectionScheme();
            if (inspectionScheme!=null){
                List<InspectionSchemeLine> code = JpaUtil.linq(InspectionSchemeLine.class).equal("schemeCode", inspectionScheme.getCode()).list();
                inspectionScheme.setLineList(code);
            }
        }
        for(Item item : page.getContent()){
            item.setInspectionSchemeChains(chainMap.get(item.getItemCode()));
        }

        return page;
    }

    @Override
    @Transactional(readOnly = false)
    public int removeByScheme(String schemeCode, Object[] itemCode,String dataScope) {
        return JpaUtil.lind(InspectionSchemeChain.class).equal("schemeCode", schemeCode).equal("dataScope", dataScope).in("itemCode", itemCode).delete();
    }

    @Override
    @Transactional(readOnly = false)
    public int removeByItem(String itemCode, Object[] schemeCode) {
        return JpaUtil.lind(InspectionSchemeChain.class).equal("itemCode", itemCode).in("schemeCode", schemeCode).delete();
    }

    @Override
    @Transactional(readOnly = true)
    public Page<Item> withoutItemByScheme(Pageable pageAble, Criteria c) {
        return JpaUtil
                .linq(Item.class)
                .notExists(InspectionSchemeChain.class)
                .equalProperty("itemCode", "itemCode")
                .equal("busiType","1")//入库质检方案
                .end()
                .where(c)
                .paging(pageAble);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<Item> getItemPageFromSchemeCode(Pageable pageAble, String schemeCode) {
        return JpaUtil
                .linq(Item.class)
                .exists(InspectionSchemeChain.class)
                .equalProperty("itemCode", "itemCode")
                .equal("schemeCode", schemeCode)
                .end()
                .paging(pageAble);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<ItemGroup> getItemGroupPageFromSchemeCode(Pageable pageAble, String schemeCode) {
        return JpaUtil
                .linq(ItemGroup.class)
                .exists(InspectionSchemeChain.class)
                .equalProperty("itemCode", "groupCode")
                .equal("schemeCode", schemeCode)
                .end()
                .paging(pageAble);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<ItemGroup> withoutItemGroupByScheme(Pageable pageAble, Criteria c) {
        return JpaUtil
                .linq(ItemGroup.class)
                .notExists(InspectionSchemeChain.class)
                .equalProperty("itemCode", "groupCode")
                .equal("busiType","1")//入库质检方案
                .end()
                .where(c)
                .paging(pageAble);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<ItemGroup> pageByItemGroup(Pageable pageAble, Criteria c) {
        Page<ItemGroup> page = JpaUtil.linq(ItemGroup.class).where(c).paging(pageAble);
        Set<String> codes = JpaUtil.collect(page.getContent(), "groupCode");
        List<InspectionSchemeChain> chains = JpaUtil.linq(InspectionSchemeChain.class)
                .collect("code", InspectionScheme.class,"schemeCode")
                .equal("busiType","1")
                .in("itemCode", codes).list();
        Map<String,List<InspectionSchemeChain>> chainMap = new HashMap<>();
        for(InspectionSchemeChain chain : chains){
            chainMap.computeIfAbsent(chain.getItemCode(), k -> new ArrayList<InspectionSchemeChain>());
            chainMap.get(chain.getItemCode()).add(chain);
            InspectionScheme inspectionScheme = chain.getInspectionScheme();
            if (inspectionScheme!=null){
                List<InspectionSchemeLine> code = JpaUtil.linq(InspectionSchemeLine.class).equal("schemeCode", inspectionScheme.getCode()).list();
                inspectionScheme.setLineList(code);
            }
        }
        for(ItemGroup item : page.getContent()){
            item.setInspectionSchemeChains(chainMap.get(item.getGroupCode()));
        }
        return page;
    }

    @Override
    @Transactional
    public Result<List<InspectionScheme>> getSchemeListByPath(String code, String dataScope) {
        List<InspectionScheme> list = JpaUtil.linq(InspectionScheme.class)
                .exists(InspectionSchemeChain.class)
                .equalProperty("schemeCode", "code")
                .equal("itemCode", code)
                .equal("dataScope", dataScope)
                .equal("busiType", "1")//入库质检方案
                .end().list();
        return Result.OK(list);
    }

    @Override
    @Transactional(readOnly = false)
    public int removeBySchemeCode(String schemeCode) {//方案删除时，需要清理道对应的关联关系数据
        return JpaUtil.lind(InspectionSchemeChain.class).equal("schemeCode", schemeCode).delete();
    }
}
